package com.hopu.blb_erp.controller;

import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.entity.ProductHandover;
import com.hopu.blb_erp.service.IDepartmentService;
import com.hopu.blb_erp.service.IProductHandoverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;



/**
 * <p>
 *  前端控制器
 *  前端控制器
 * </p>
 *
 *
 * @author A-Zhang
 * @since 2021-11-16
 */
@Api
@Controller
@RestController
public class ProductHandoverController {
    @Autowired
    private IProductHandoverService productHandoverService;

    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation("查询功能的分页")
    @GetMapping("/ProductHandovers")
    public ResponseEntity<PageEntity<ProductHandover>> findProductHandoverPage(@RequestParam("current") Integer current, @RequestParam("PAGE_SIZE") Integer PAGE_SIZE
            , @RequestParam("orderId") String id, @RequestParam("handoverDate") String date ) throws ParseException {
        int orderId = 0;
        if(!"".equals(id)){
             orderId = Integer.parseInt(id);
        }
        if(!"".equals(date)&&!"null".equals(date)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date handoverDate0 = simpleDateFormat.parse(date);
            Instant instant = handoverDate0.toInstant();
            ZoneId zone = ZoneId.systemDefault();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
            LocalDate handoverDate = localDateTime.toLocalDate();
            PageEntity<ProductHandover> productHandovers = productHandoverService.findAllProductHandoverPage(current,PAGE_SIZE,orderId,handoverDate);
            productHandovers.getData().forEach(p->{
                p.setDepartment(departmentService.findDepartmentByStaffId((int) p.getStaff().getdId()));
            });
            return ResponseEntity.ok(productHandovers);
        }else{
            LocalDate handoverDate = null;
            PageEntity<ProductHandover> productHandovers = productHandoverService.findAllProductHandoverPage(current,PAGE_SIZE,orderId,handoverDate);
            productHandovers.getData().forEach(p->{
                p.setDepartment(departmentService.findDepartmentByStaffId((int) p.getStaff().getdId()));
            });
            return ResponseEntity.ok(productHandovers);
        }
    }

    @ApiOperation("查询所有")
    @GetMapping("/productHandoverss")
    public ResponseEntity<List<ProductHandover>> findAllProductHandover(){
        List<ProductHandover> allProductHandover = productHandoverService.findAllProductHandover();
        return ResponseEntity.ok(allProductHandover);
    }

    @ApiOperation("添加")
    @PostMapping("/ProductHandover/{order2Id}")
    public ResponseEntity<String> addProductHandover(@RequestBody ProductHandover productHandover,@PathVariable("order2Id") Integer order2Id){
        productHandoverService.addProductHandover(productHandover,order2Id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("删除")
    @DeleteMapping("/ProductHandover")
    public ResponseEntity<String> deleteProductHandovers(@RequestParam(value = "delThird",required = false) Integer[] delThird){
        productHandoverService.removeByIds(Arrays.asList(delThird));
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("修改交接单表状态")
    @PutMapping("/ProductHandover/{id}/{order2Id}")
    public ResponseEntity<String> update(@PathVariable("id") Integer id,@PathVariable("order2Id") Integer order2Id){
        productHandoverService.updateProductHandover(id,order2Id);
        return ResponseEntity.ok("ok");
    }

}
